package com.lin.operatorlogrecord.entity.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import com.lin.operatorlogrecord.entity.constant.TableNameConstant;
import com.lin.operatorlogrecord.exception.EnumException;
import lombok.Getter;

/**
 * @Author: chenghao.lin
 * @Date: 2023/09/21
 * @Description:
 */
@Getter
public enum ObjectTypeEnum implements IEnumBase{
    USER(0, "用户", TableNameConstant.USER),
    OPERATOR_LOG(1, "操作日志", TableNameConstant.OPERATOR_LOG),
    ROLE(2, "角色", TableNameConstant.ROLE),
    STUDENT(3, "学生", TableNameConstant.STUDENT);

    @EnumValue
    @JsonValue
    private final Integer code;

    private final String name;

    private final String tableName;

    ObjectTypeEnum(Integer code, String name, String tableName) {
        this.code = code;
        this.name = name;
        this.tableName = tableName;
    }

    @Override
    public IEnumBase convertIEnumBase(Integer code) {
        for (ObjectTypeEnum objectTypeEnum : ObjectTypeEnum.values()) {
            if (objectTypeEnum.getCode().equals(code)) {
                return objectTypeEnum;
            }
        }
        throw new EnumException("该整型值没有对应枚举：" + code);
    }


    public static ObjectTypeEnum getByTableName(String tableName) {
        for (ObjectTypeEnum objectTypeEnum : ObjectTypeEnum.values()) {
            if (objectTypeEnum.getTableName().equals(tableName)) {
                return objectTypeEnum;
            }
        }
        throw new EnumException("该表名没有对应枚举：" + tableName);
    }


}
